package bb.lanxing.util.jsbridge;

import android.text.TextUtils;
import android.util.Log;
import android.webkit.JavascriptInterface;
import java.util.Map;

public class BaseJavascriptInterface {
    private BridgeHelper mBridgeHelper;
    private Map<String, CallBackFunction> mCallbacks;
    private Map<String, BridgeHandler> mMessageHandlers;

    public BaseJavascriptInterface(BridgeHelper helper) {
        this.mCallbacks = helper.getResponseCallbacks();
        this.mMessageHandlers = helper.getMessageHandlers();
        this.mBridgeHelper = helper;
    }

    public BaseJavascriptInterface(BridgeWebView webView) {
        this(webView.getBridgeHelper());
    }

    @JavascriptInterface
    public final String send(String data, String callbackId) {
        Log.d("chromium", data + ", callbackId: " + callbackId + " " + Thread.currentThread().getName());
        return receiveMessage(data, callbackId);
    }

    @JavascriptInterface
    @Deprecated
    public final void send(String handlerName, String data, final String callbackId) {
        Log.d("chromium", "handlerName: " + handlerName + " " + data + ", callbackId: " + callbackId + " " + Thread.currentThread().getName());
        BridgeHandler bridgeHandler = this.mMessageHandlers.get(handlerName);
        final BridgeHelper bridgeHelper = this.mBridgeHelper;
        if (bridgeHandler == null && bridgeHelper != null) {
            bridgeHandler = bridgeHelper.getDefaultHandler();
        }
        if (bridgeHandler != null) {
            bridgeHandler.handler(data, new CallBackFunction() {
                @Override
                public void onCallBack(String data2) {
                    BridgeHelper bridgeHelper2 = bridgeHelper;
                    if (bridgeHelper2 == null || data2 == null) {
                        return;
                    }
                    bridgeHelper2.sendResponse(data2, callbackId);
                }
            });
        }
    }

    @JavascriptInterface
    public final void response(String data, String responseId) {
        Log.d("chromium", data + ", responseId: " + responseId + " " + Thread.currentThread().getName());
        responseFormWeb(data, responseId);
    }

    protected String receiveMessage(String data, final String callbackId) {
        final BridgeHelper bridgeHelper = this.mBridgeHelper;
        BridgeHandler defaultHandler = bridgeHelper != null ? bridgeHelper.getDefaultHandler() : null;
        if (defaultHandler != null) {
            defaultHandler.handler(data, new CallBackFunction() {
                @Override
                public void onCallBack(String data2) {
                    if (data2 != null) {
                        bridgeHelper.sendResponse(data2, callbackId);
                    }
                }
            });
        }
        return null;
    }

    protected void responseFormWeb(String data, String responseId) {
        CallBackFunction remove;
        if (TextUtils.isEmpty(responseId) || (remove = this.mCallbacks.remove(responseId)) == null) {
            return;
        }
        remove.onCallBack(data);
    }
}
